package com.dream.dao.master;

/**
 * 
 * @author Evan
 * @date 2013-8-25
 * 
 */
import java.util.List;

import org.springframework.stereotype.Repository;

import com.dream.model.master.ShopUser;
import com.dream.utils.hibernate.DetachedCriteriaBuilder;
import com.dream.utils.hibernate.DreamBaseDAO;
import com.dream.utils.hibernate.PagingVO;
import com.dream.vo.master.ShopVO;

@Repository
public class ShopUserDAO extends DreamBaseDAO<ShopUser> {
	
	public List<ShopUser> search(final String userId,final String shopId){
		
		DetachedCriteriaBuilder builder = DetachedCriteriaBuilder.instance(ShopUser.class);
		builder.innerJoin("shop", "shop").innerJoin("appUser", "user");
		builder.addEq("deleted", false).addEq("userId", userId);
		return this.select(builder);
		
	}
	
	public List<ShopUser> searchMyShop(final String userId){
		
		DetachedCriteriaBuilder builder = DetachedCriteriaBuilder.instance(ShopUser.class);
		builder.addEq("deleted", false).addEq("userId", userId);
		return this.select(builder);
		
	}
	
	public PagingVO query(ShopVO vo, PagingVO page){
		
		DetachedCriteriaBuilder queryBuilder = initQueryBuilder(vo);
		DetachedCriteriaBuilder countBuilder = initQueryBuilder(vo);
		return this.selectPagingVO(queryBuilder, page, countBuilder);
	}
	

	private DetachedCriteriaBuilder initQueryBuilder(final ShopVO vo){
		
		DetachedCriteriaBuilder builder = DetachedCriteriaBuilder.instance(ShopUser.class);
		builder.innerJoin("appUser", "au");
		builder.addEq("deleted", false).addEq("shopId", vo.getId());
		builder.addEq("au.id", vo.getUserId()).addLikeAny("au.name", vo.getUserName());
		return builder;
		
	}
	
}
